package com.daikuan.fffkuaiban.ui.fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.daikuan.fffkuaiban.adapter.XinyongKaListAdapter;
import com.daikuan.fffkuaiban.bmobBean.ClickBean;
import com.daikuan.fffkuaiban.bmobBean.Creditbean;
import com.daikuan.fffkuaiban.ui.activity.WebViewActivity;
import com.jarhead.common.base.BaseFragment;
import com.daikuan.fffkuaiban.R;
import com.daikuan.fffkuaiban.ui.activity.TinyWebView;
import com.lcodecore.tkrefreshlayout.Footer.LoadingView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import org.json.JSONArray;

import java.util.List;

import butterknife.Bind;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by jiming1 on 2017/8/3.
 */
public class ThirdFragment extends BaseFragment {

	@Bind(R.id.refresh)
	TwinklingRefreshLayout refreshLayout;
	@Bind(R.id.listView)
	ListView listView;
	XinyongKaListAdapter xinyongKaListAdapter;

	@Override
	protected int getLayoutResource() {
		return R.layout.fragment_third;
	}

	@Override
	public void initPresenter() {

	}

	@Override
	protected void initView() {
		SinaRefreshView headerView = new SinaRefreshView(getActivity());
		headerView.setArrowResource(R.mipmap.arrow);
		headerView.setTextColor(0xff745D5C);
		refreshLayout.setHeaderView(headerView);
		refreshLayout.setEnableLoadmore(false);
		View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.item_footer, null);
		listView.addFooterView(footerView);
		View headView = LayoutInflater.from(getActivity()).inflate(R.layout.item_header_view, null);
		listView.addHeaderView(headView);
		refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
			@Override
			public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
				initData();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						refreshLayout.finishRefreshing();
					}
				}, 2000);
			}
		});


		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Log.e("XXX-pos",position+"");
				if (position == 0 || position == xinyongKaListAdapter.getCount() - 1)
					return;
				Creditbean creditbean = xinyongKaListAdapter.getItem(position - 1);
				WebViewActivity.skip(getActivity(), creditbean.link, creditbean.title);
			}
		});
	}

	@Override
	protected void initData() {

		BmobQuery query = new BmobQuery("credits");
		query.order("createdAt");
		query.findObjectsByTable(new QueryListener<JSONArray>() {
			@Override
			public void done(JSONArray ary, BmobException e) {
				try {
					if (e == null) {
						String str = ary.toString();
						Log.e("XXX-top_baner", str);
						List<Creditbean> clickBeens = JSON.parseArray(str, Creditbean.class);
						if (clickBeens != null || clickBeens.size() > 0) {
							xinyongKaListAdapter = new XinyongKaListAdapter(getActivity(), clickBeens);
							listView.setAdapter(xinyongKaListAdapter);
						}
					} else {
//						ToastHelper.toast("获取数据失败");
					}
				} catch (Exception e2) {
//					ToastHelper.toast("获取数据失败");
				}
			}
		});

	}
}
